package com.learm.socket.pool_v2;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import org.apache.log4j.Logger;

/**
 * <p>
 * Title: socket连接的简单实现
 * </p>
 *
 * <p>
 * Description:
 * </p>
 *
 * <p>
 * Copyright: 融博技术有限公司 2012
 * </p>
 *
 * @author 袁泉锋HO174959
 * @version 1.0
 * @date Oct 15, 2012
 *
 */
public class SocketConnection extends Socket {
    private static final Logger logger = Logger.getLogger("FrameLog");
    /**
     * status false 繁忙 true 空闲
     */
    private boolean status = true;

    /**
     *   默认的构造函数
     */
    public SocketConnection() {
        super();
    }

    /**
     * ConnectionAdapter constructor
     */
    public SocketConnection(String host, int port)
            throws UnknownHostException, IOException {
        super(host, port);
    }

    /**
     * <Description>判断此连接是否空闲
     *
     * @since Oct 15, 2012
     * @return <Description>空闲返回ture,否则false
     *
     */
    public boolean isFree() {
        return status;
    }

    /**
     * <Description> 当使用此连接的时候设置状态为false（忙碌）
     *
     * @since Oct 15, 2012 <Description>
     *
     */
    public void setBusy() {
        this.status = false;
    }

    /**
     * <Description>当客户端关闭连接的时候状态设置为true(空闲）,放回连接池
     *
     * @since Oct 15, 2012 <Description>
     *
     */
    public void close() {
        logger.info("ConnectionAdapter Close : set   the   status   is   free");
        status = true;
    }

    /**
     * <Description>销毁连接
     * @throws IOException
     *
     * @since Oct 15, 2012 <Description>
     *
     */
    public void destroy() throws IOException {
        //Close   socket   connection
         super.close();
        logger.info("ConnectionAdapter destroy!");
        // System.out.println( "Close   success   "   );
    }
}
